
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:h_flutter_119/mixin/toast_mixin.dart';
import 'package:h_flutter_119/page/find/EyeFindPage.dart';
import 'package:h_flutter_119/page/home/HomePage.dart';
import 'package:h_flutter_119/page/mine/mine2.dart';
import 'package:h_flutter_119/page/wan/WanHomePage.dart';
import 'package:h_flutter_119/res/colors.dart';
import 'package:h_flutter_119/route/route_utils.dart';

import 'mine/MinePage.dart';

class MainPage extends StatefulWidget{
  const MainPage({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> with ToastMixin{

  final PageController _controller = PageController(initialPage: 0);
  DateTime? _dateTime;
  int _currentIndex = 0;
  bool isShowFloatBt = false;
  bool isShowMore = false;
  List<String> titles = ["首页","WanAndroid","发现","我的"];
  List<Widget> pages = [HomePage(), WanHomePage(), EyeFindPage(), MinePage()];
  List<BottomNavigationBarItem> barItems = [
    BottomNavigationBarItem(icon: Icon(Icons.home),label: 'Eye'),
    BottomNavigationBarItem(icon: Icon(Icons.explore),label: 'Wan'),
    BottomNavigationBarItem(icon: Icon(Icons.find_in_page),label: '发现'),
    BottomNavigationBarItem(icon: Icon(Icons.person),label: '我的')
  ];
  List<BottomNavigationBarItem> barItems2 =
  [
    BottomNavigationBarItem(icon: Icon(Icons.home),label: 'Eye'),
    BottomNavigationBarItem(icon: Icon(Icons.explore),label: 'Wan'),
    BottomNavigationBarItem(icon: Icon(Icons.find_in_page),label: '发现'),
    BottomNavigationBarItem(icon: Icon(Icons.person),label: '我的'),
    BottomNavigationBarItem(icon: Icon(Icons.account_tree),label: 'GetXPage')
  ];
  String title = "首页";
  @override
  Widget build(BuildContext context) {

    return WillPopScope(
        onWillPop: _willPopCallback,
        child: Scaffold(
          appBar: AppBar(
            title: Text(title),
            actions: [
              IconButton(onPressed: () => goSearchPage(context), icon: Icon(Icons.search))
            ],
          ),
          body: PageView(
            onPageChanged: _pageChange,
            controller: _controller,
            physics:  NeverScrollableScrollPhysics(),
            children: pages,
          ),
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: _currentIndex,
            items: isShowMore ? barItems2 : barItems,
            elevation: 8.0,
            type: BottomNavigationBarType.fixed,
            fixedColor: Colors.blue,
            onTap: _onMainTab,
          ),
          floatingActionButton: Visibility(
            visible: isShowFloatBt,
            child: FloatingActionButton(
              onPressed: () {
                setState((){
                  isShowMore = true;
                  titles.add('动态添加GetXPage');
                  pages.add(GetXPage());
                  _onMainTab(titles.length-1);
                  _pageChange(titles.length-1);
                });
              },
              child:  Icon(Icons.add,color: ColorStyle.color_EA4C43),
            ),
          ),
        ));
  }

  Future<bool> _willPopCallback() async{
    if(_dateTime == null || DateTime.now().difference(_dateTime!) > const Duration(seconds: 2)) {
      _dateTime = DateTime.now();
      showShortToast('再按一次退出');
      return false;
    }else {
      _dateTime = DateTime.now();
      // 退出app
      await SystemNavigator.pop();
      return true;
    }
  }


  void _pageChange(int value) {
    setState((){
      _currentIndex = value;
      title = titles[value];
      isShowFloatBt = value == titles.indexOf('我的') && titles.length == 4;
    });
  }

  void _onMainTab(int value) {
    _controller.jumpToPage(value);
    setState((){
      _currentIndex = value;
    });
    // _controller.animateToPage(value, duration: const Duration(milliseconds: 300), curve: Curves.ease);
  }
}